﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web;
using System.Web.Services;

namespace Webservice20160829
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod(Description = "直接返回DataSet对象")]
        public DataSet GetDataSet()
        {
            string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["MSSSqlserver"].ToString();
            SqlConnection conn = new SqlConnection(connStr);
            string sql = "select * from MyFirstDatabase.[dbo].[医生_专科病历片语维护]";
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet("China");          
            sda.Fill(ds);
            conn.Close();
            return ds;
        }
        [WebMethod(Description = "直接返回DataSet对象，并用Binary序列化后的字节数组")]
        public byte[] GetDataSetBytes()
        {
            DataSet ds = GetDataSet();
            BinaryFormatter ser = new BinaryFormatter();  //序列化对象
            MemoryStream ms = new MemoryStream();  //内存流
            ser.Serialize(ms, ds);
            byte[] buffer = ms.ToArray();    //字节流
            return buffer;
        }
    }
}
